import groovy.sql.Sql
import java.sql.SQLException

public execute(o) {
    return new Mc(o)
}

class Mc {

    def mypara
    def datalist = []
    def itemlist = []
    def selectedLog = null
    def wmsReport = ""
    def requestData = ""
    def lbselectrow = null  // 添加 lbselectrow 属性

    public Mc(f) {
        this.mypara = f
    }

    def select() {
        def conn = null
        try {
            datalist = []
            conn = Sql.newInstance(mypara.hqcon(1))
            def query = """
                SELECT id, code, message, success, state, Ascript, time 
                FROM wms_bainuo_log
                ORDER BY time DESC
            """
            conn.eachRow(query) { row ->
                def t = [
                        id: row.id,
                        code: row.code,
                        message: row.message == "成功" ? "执行完成" : "执行未成功",
                        success: row.success == "成功" ? "是" : "否",
                        state: row.state,
                        Ascript: row.Ascript,
                        time: row.time
                ]
                datalist.add(t)
            }
            mypara.msg("查询成功")
            println(11111)
        } catch (SQLException e) {
            e.printStackTrace()
            println("SQL 错误: ${e.message}")
        } catch (Exception e) {
            e.printStackTrace()
            println("发生错误: ${e.message}")
        } finally {
            if (conn != null) {
                conn.close()
            }
        }
    }

    def queryWMSReport(logId) {
        def conn = null
        try {
            conn = Sql.newInstance(mypara.hqcon(1))
            def query = """
                SELECT response 
                FROM wms_bainuo_log
                WHERE id = ?
            """
            conn.eachRow(query, [logId]) { row ->
                wmsReport = row.response
            }
            if (wmsReport) {
                mypara.msg("查询成功")
            } else {
                mypara.msg("未找到报文内容")
            }
        } catch (SQLException e) {
            e.printStackTrace()
            println("SQL 错误: ${e.message}")
        } catch (Exception e) {
            e.printStackTrace()
            println("发生错误: ${e.message}")
        } finally {
            if (conn != null) {
                conn.close()
            }
        }
    }

    def queryRequestData(logId) {
        def conn = null
        try {
            conn = Sql.newInstance(mypara.hqcon(1))
            def query = """
                SELECT request_data 
                FROM wms_bainuo_log
                WHERE id = ?
            """
            conn.eachRow(query, [logId]) { row ->
                requestData = row.request_data
            }
            if (requestData) {
                mypara.msg("查询成功")
            } else {
                mypara.msg("未找到传入数据")
            }
        } catch (SQLException e) {
            e.printStackTrace()
            println("SQL 错误: ${e.message}")
        } catch (Exception e) {
            e.printStackTrace()
            println("发生错误: ${e.message}")
        } finally {
            if (conn != null) {
                conn.close()
            }
        }
    }
}